package com.touch.jgloo.owt;
import java.awt.event.*;
import com.touch.graphics.Point3D;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
/**
 * JGLFrame is a JGLContainer that allows other JGLComponents to be added to it, in addition to having a titlebar.
 * The Titlebar of a JGLFrame contains the caption, close button, and hide/show button. * 
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public class JGLFrame extends JGLContainer
{
	//label for the  caption
	private final JGLLabel 	caption;
	//panel for the title bar
	private final JGLPane	titleBar;
	//main panel
	private final JGLPane 	panel;
	//close button
	private final JGLButton close;
	//minimize/maximize button
	private final JGLButton	hideShow;
	//point for the maximized position
	private final Point3D maximizedPos = new Point3D();
	//temporary point for movement
	private final Point3D movePos = new Point3D();
	//value determining if the JGLFrame is minimized
	private boolean minimized = false;
	//determines if the window is being dragged
	private boolean drag = false;
	/**
	 * Builds a JGLFrame. To successfully create a JGLFrame, a caption, width, and height must be defined.
	 * The Caption is what is displayed on the titlebar. Width and height represent the absolute size of the form, this includes the border.
	 * @since r461
	 */
	public JGLFrame(final String caption, final int width, final int height)
	{
		//create the caption
		this.caption 	= new JGLLabel(caption);
		//create the main panel
		this.panel 		= new JGLPane(width, height);
		//create the title bar
		this.titleBar	= new JGLPane(width, 16);
		//create the close button
		this.close		= new JGLButton("X ", 24, 16);
		//create the min/max button
		this.hideShow	= new JGLButton("v ", 24, 16);
		
		//add the title bar to the main panel
		this.panel.add(this.titleBar);
		//add the caption to the main panel
		this.panel.add(this.caption);
		//add the close button to the main panel
		this.panel.add(this.close);
		//add the min/max button to the main panel
		this.panel.add(this.hideShow);
				
		//set the size of the JGLFrame
		this.setSize(width, height);
		//set the default visibility to nothing
		this.setVisible(false);
	}
	/**
	 * Returns the close button
	 * @return JGLButton for close operations
	 * @since r461
	 */
	public final  JGLButton getCloseButton()
	{
		//return the close button
		return this.close;
	}
	/**
	 * Returns the min/max button
	 * @return JGLButton used for minimizing/maximizing the window
	 * @since r461
	 */
	public final  JGLButton getHideShowButton()
	{
		//return the min/max button
		return this.hideShow;
	}
	
	public final  void applyStyleSheet(final JGLStyleSheet ss) 
	{	
		this.caption.applyStyleSheet(ss);
		this.panel.applyStyleSheet(ss);
		this.titleBar.applyStyleSheet(ss);
		this.close.applyStyleSheet(ss);
		this.hideShow.applyStyleSheet(ss);
	
	}

	
	public final  void repositioned(int x, int y, int z) 
	{	
		this.panel.setPosition(x, y, z);
	}

	
	public final  void resized(int w, int h) 
	{
		this.caption.setPosition(
			this.titleBar.getSize().x / 2 - this.caption.length() * 5, 0, 0);
		this.close.setPosition(this.panel.getSize().x - 24, 0, 0);
		this.hideShow.setPosition(this.panel.getSize().x - 48, 0, 0);		
	}
	
	public final  void render() 
	{	
		if(this.isVisible() == false)
			return;
		GL_INSTANCE.glPushMatrix();
			panel.render();
		GL_INSTANCE.glPopMatrix();
	}
	
	public final  void clean() 
	{	
		//clean the panel
		panel.clean();
	}
	
	public final  void compile() 
	{
		//compile the panel
		panel.compile();
	}	
	
	public final void mouseClicked(final MouseEvent e) 
	{
		//determine if the window is visible
		if(this.isVisible() == false)
			//otherwise return
			return;
		//get the x position of the mouse event
		final int x = e.getX();
		//get the y position of the mouse event
		final int y = e.getY();
		//get the position of the JGLFrame
		final Point3D position = this.getPosition();
		//get the size of the JGLFrame
		final Point3D size = this.getSize();
		
		//determine if the JGLFrame is clicked
		if(x > position.x && x < (position.x + size.x) && y > position.y && y < (position.y + size.y))
		{
			//set the JGLFrame as being selected
			setSelected(this);
			//fire the close button's mouse events
			this.close.mouseClicked(e);
			//fire the hide/show button's mouse events
			this.hideShow.mouseClicked(e);
			//if the close button's selected
			if(getSelected() == this.close.getIndex())
				//close the window
				this.setVisible(false);
			//if the minimize/maximize button  is selected
			if(getSelected() == this.hideShow.getIndex())
			{
				//if the window is maximized
				if(this.minimized == false)
				{
					//set the position to minimized
					this.maximizedPos.x = this.getPosition().x;
					this.maximizedPos.y = this.getPosition().y;
					this.maximizedPos.z = this.getPosition().z;
					
					//set the position of the object
					this.setPosition(this.getPosition().x, 464, 0);
					//change the caption
					this.hideShow.setCaption("^");
					//
					this.minimized = true;
				}
				else
				{
					//set the window to maximized
					this.setPosition(
						this.maximizedPos.x,
						this.maximizedPos.y,
						this.maximizedPos.z);
					this.minimized = false;
					//change the caption
					this.hideShow.setCaption("v");
				}						
			}
		}				
	}
	
	public final void mousePressed(final MouseEvent e) 
	{			
		//determine if the JGLFrame is visible
		if(this.isVisible() == false)
			return;		
		//get the x value for position
		final int x = e.getX();
		//get the y value for position
		final int y = e.getY();
		//get the position
		final Point3D position = this.getPosition();
		//get the size
		final Point3D size = this.getSize();
				
		//determine if the titlebar is being clicked
		if(x > position.x && x < (position.x + size.x) && y > position.y && y < (position.y + size.y))
		{
			//fire the titlebar's mouse events
			this.titleBar.mouseClicked(e);
			//if the titlebar is selected
			if(getSelected() == this.titleBar.getIndex())
			{				
				//start dragging
				drag = true;
				//reposition the window
				movePos.x = e.getX() - this.getPosition().x;
				movePos.y = e.getY() - this.getPosition().y;
			}
		}				
	}	
	
	public final void mouseReleased(final MouseEvent e) 
	{
		//determine if the component is visible
		if(this.isVisible() == false)
			//return whether the component is visible
			return;
		//determine if the titlebar is selected
		if(getSelected() == this.titleBar.getIndex())
		{
			//if it is currently being dragged
			if(this.drag)
			{
				//set the new position
				this.setPosition(
					e.getX() - this.movePos.x,
					e.getY() - this.movePos.y,
					this.getPosition().z);
				//stop dragging
				drag = false;
			}
		}
	}
	
	public final  void add(final JGLComponent component) 
	{
		//add the panel to the list
		this.panel.add(component);
		//set the component to show if this JGLFrame is shown
		component.setVisible(this.isVisible());
	}
	
	public final void removeComponents() 
	{	
		//removes the components
		this.panel.removeComponents();
	}	
	
	public final void setVisible(boolean v)
	{
		//sets the visibility of the JGLFrame
		super.setVisible(v);
		//set the visibility of the panel
		panel.setVisible(v);
	}
	public JGLFrame clone()
	{
		final JGLFrame clone = new JGLFrame(this.caption.getText(), this.getWidth(), this.getHeight());
		clone.minimized = this.minimized;
		clone.setPosition(this.getPositionX(), this.getPositionY(), this.getPositionZ());
		clone.setParentPosition(this.getParentPosition().x, this.getParentPosition().y, this.getParentPosition().z);
		clone.setVisible(this.isVisible());
		
		return clone;
	}
}
